// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.material.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Material;
import com.liuxinlong.modules.material.controller.request.ImportMaterialReq;
import com.liuxinlong.modules.material.controller.request.QueryMaterialReq;
import com.liuxinlong.modules.material.controller.request.UpdateMaterialReq;
import com.liuxinlong.modules.material.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物料管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年2月17日
 */
@RestController
@RequestMapping("/material")
public class MaterialController {
	@Autowired
	private MaterialService materialService;

	@Autowired
	private UploadService uploadService;

	/**
	 * 分页查询物料信息
	 * 
	 * @param req 请求信息
	 * @return 物料信息列表
	 */
	@PostMapping("page")
	public BaseResp pageInfo(@RequestBody QueryMaterialReq req) {
		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put("startNum", req.getStartNum());
		queryMap.put("pageSize", req.getPageSize());
		List<Map<String, Object>> dataList = materialService.pageMaterialList(queryMap);
		int totalNum = materialService.getMaterialCount(queryMap);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("分页查询物料信息成功");
		resp.setDataList(dataList);
		resp.setTotalNum(totalNum);
		return resp;
	}

	/**
	 * 新增物料信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("add")
	@LogAnnotation(value ="新增物料信息",business = LogBusinessConstants.MATERIAL_MANAGE,operateType = LogOperateConstants.OPERATE_ADD)
	public BaseResp addMaterialInfo(@RequestBody UpdateMaterialReq req) {
		Material material = new Material();
		material.setSn(req.getSn());
		material.setName(req.getName());
		material.setRemark(req.getRemark());
		materialService.addMaterial(material);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("新增物料信息成功");
		return resp;

	}

	/**
	 * 修改物料信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("update")
	@LogAnnotation(value ="修改物料信息",business = LogBusinessConstants.MATERIAL_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateMaterialInfo(@RequestBody UpdateMaterialReq req) {
		Material material = new Material();
		material.setId(req.getId());
		material.setSn(req.getSn());
		material.setName(req.getName());
		material.setRemark(req.getRemark());
		materialService.updateMaterial(material);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改物料信息成功");
		return resp;
	}

	/**
	 * 删除物料信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("delete")
	@LogAnnotation(value ="删除物料信息",business = LogBusinessConstants.MATERIAL_MANAGE,operateType = LogOperateConstants.OPERATE_DELETE)
	public BaseResp deleteMaterialInfo(@RequestBody BaseDeleteReq req) {
		materialService.deleteMaterial(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("删除物料信息成功");
		return resp;
	}

	/**
	 * 批量修改物料状态信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("status-batch-update")
	@LogAnnotation(value ="批量修改物料状态信息",business = LogBusinessConstants.MATERIAL_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp batchUpdateStatus(@RequestBody BaseDeleteReq req) {
		materialService.deleteMaterial(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("批量修改物料状态成功");
		return resp;
	}

	/**
	 * 图纸文件同步
	 *
	 * @return 成功标识
	 */
	@GetMapping("file-sync")
	@LogAnnotation(value ="图纸文件同步",business = LogBusinessConstants.MATERIAL_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp syncDrawFile() {
		int total = materialService.syncDrawFile();
		BaseResp resp = new BaseResp();
		resp.setRetMessage("图纸文件同步成功！");
		resp.setTotalNum(total);
		return resp;
	}

	/**
	 * 导入数据预处理
	 *
	 * @param multipartRequest 文件
	 * @return 数据
	 */
	@PostMapping("import")
	@LogAnnotation(value ="导入物料信息数据",business = LogBusinessConstants.MATERIAL_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
	public ImportResp importMaterials(MultipartRequest multipartRequest) {
		Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
		ImportResp resp = materialService.importMaterials((String) map.get("fileRealPath"));
		return resp;
	}

	/**
	 * 导入物料信息信息
	 *
	 * @param req 请求体
	 * @return 返回导入失败数据
	 */
	@PostMapping("batch-add")
	@LogAnnotation(value ="导入物料信息数据",business = LogBusinessConstants.MATERIAL_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
	public ImportResp batchAddMaterial(@RequestBody ImportMaterialReq req) {
		return materialService.batchAddMaterial(req.getDataList());
	}
}
